package ru.vboxdc.service;

import ru.vboxdc.ObjectPool;
import ru.vboxdc.domain.HostServer;

import javax.servlet.http.HttpServlet;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

/**
 * User: lelon
 * Date: 30.01.2009
 * Time: 18:31:43
 */
public class StartupServlet extends HttpServlet {

    private static Logger logger = Logger.getLogger(StartupServlet.class);

    public StartupServlet() {
        try {
            //Get a list of servers from the database
            new InstallDataBase().run();
            List<String> hostServersConfig = SqlMapper.get().queryForList("SelectAllServers");
            logger.debug("hostServers.size=" + hostServersConfig.size());
            //    List<HostServer> hostServers = new ArrayList();
            ObjectPool objectPool = ObjectPool.get();
            ManagerFactory managerFactory = new ManagerFactory();
            for (String hostServerName : hostServersConfig) {
                //Get a list of parameters from the database
                Map parametersMap = SqlMapper.get().queryForMap("SelectParameterForServer", hostServerName, "key", "value");
                //Create  hostserver object , and fill it list of virtual machines
                HostServer hostServer = new HostServer();
                //Set manager for hostserver  object
                hostServer.setManager(managerFactory.createHostServerManager(parametersMap));
                hostServer.refreshVirtualMachines();
                hostServer.setName(hostServerName);
                //Add hostserver object in the list of servers from objectpool
                objectPool.addHostServer(hostServer);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.debug("StartupServlet end");
    }
}
